//    Copyright (C) 2008 Dirk Vanden Boer <dirk.vdb@gmail.com>
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include <stdlib.h>

#include "audiothumbnailer.h"

using namespace std;

void printUsage();

int main(int argc, char** argv)
{
    int     option;
    int     thumbnailSize = 128;
    char*   inputFile = NULL;
    char*   outputFile = NULL;
    bool    coverOverlay = false;

    while ((option = getopt (argc, argv, "i:o:s:t:ch")) != -1)
    {
        switch (option)
        {
            case 'i':
                inputFile = optarg;
                break;
            case 'o':
                outputFile = optarg;
                break;
            case 's':
                thumbnailSize = atoi(optarg);
                break;
            case 'c':
                coverOverlay = true;
                break;
            case 'h':
                printUsage();
                return 0;
            case '?':
            default:
                cerr << "invalid arguments" << endl;
                printUsage();
                return -1;
        }
    }

    if (!inputFile || !outputFile)
    {
        cerr << "invalid arguments" << endl;
        printUsage();
        return 0;
    }

    try
    {
        AudioThumbnailer audioThumbnailer(thumbnailSize, coverOverlay);
        audioThumbnailer.generateThumbnail(inputFile, outputFile);
    }
    catch (exception& e)
    {
        cerr << "Error: " << e.what() << endl;
        return -1;
    }
    catch (...)
    {
        return -1;
    }

    return 0;
}

void printUsage()
{
    cout << "Usage: audiothumbnailer [options]" << endl << endl
         << "Options:" << endl
         << "  -i<s>  : input file" << endl
         << "  -o<s>  : output file" << endl
         << "  -s<n>  : thumbnail size (default: 128)" << endl
         << "  -c     : put the thumbnail on a cd case (default: disabled)" << endl
         << "  -h     : display this help" << endl;
}
